package com.jketing.controller.ams.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jketing.controller.BaseController;
import com.jketing.entity.ams.base.Organ;
import com.jketing.service.ams.base.DictionaryService;
import com.jketing.service.ams.base.OrganService;
import com.jketing.utils.BeanUtil;
import com.jketing.utils.ImageUtil;

/**
 * Controller 请求处理
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-11-22
 * @since   Spring-mvc 4.3.8
 */
@Controller
@RequestMapping("/organ")
public class OrganController extends BaseController<Organ,String>{

	/**
	 *  service 服务层引用
	 */
	private OrganService service;
	/**
	 *  service 服务层自动注入
	 */
	@Autowired
	private DictionaryService ds;
	/**
	 *  service 自动注入服务层操作实例
	 */
	@Autowired
	public void setService(OrganService service) {
		super.setBaseService(service);
		this.service = service;
	}
	
	@Override
	protected String getPrefixPath() {
		return "base/organ/";
	}

	@Override
	protected String getQueryMode() {
		return "Entity";
	}
	
	/**
	 * 初始化单选对话框
	 */
	@Override
	protected void initSingleSelect(Map<String, Object> map) {
		List<Organ> list = service.getOrganList("unit");//加载单位列表
		map.put("list", list);
		map.put("topTree",service.getTree(service.getRootId()).toString());
	}

	/**
	 * 初始化多选对话框
	 */
	@Override
	protected void initMultiSelect(Map<String, Object> map) {
		List<Organ> list = service.getOrganList("unit");//加载单位列表
		map.put("list", list);
		map.put("topTree",service.getTree(service.getRootId()).toString());
	}
	
	/**
	 * 初始化区域列表框架页
	 */
	@Override
	protected void initListFrame(Map<String, Object> map) {
		List<Organ> list = service.getOrganList("unit");//加载单位列表
		map.put("list", list);
		map.put("topTree", service.getTree((String)map.get("rootId")));//加载从id为根节点开始的树
	}
	
	@Override
	protected Organ getQuery(Organ query) {
		String type = (String)getSession().getAttribute("region_type");
		if("redirect".equals(type)) {
			request.setAttribute("type", type);
			if("Entity".equals(getQueryMode())) {
				query = (Organ)getSession().getAttribute("region_query");
			}
		}
		request.setAttribute("unitNature", ds.getPropertyByEnName("unitNature"));
		request.setAttribute("subOrdination", ds.getPropertyByEnName("subOrdination"));
		request.setAttribute("classifyToManage", ds.getPropertyByEnName("classifyToManage"));		
		//可在此处增加修改查询条件的代码
		query = BeanUtil.setObjectEmptyFieldToNull(query);
		return query;
	}
	
	@Override
	protected Organ getEntity(Organ entity) {
		entity.setClassifyToManage(null);
		entity.setRegion(null);
		entity.setSubOrdination(null);
		entity.setUnitNature(null);
		return entity;
	}

	/**
	 * 异步请求树型子节点，返回json数据
	 * 
	 * xml配置了mvc:message-converters，全局编码统一转换，则不需要produces
	 * 
	 * @return 无返回，直接向response里写数据
	 * @throws IOException 
	 */
	@RequestMapping(value="/getTree",method=RequestMethod.GET)
	@ResponseBody
	public String getTree(String id){
		return service.getTree(id).toString();
	}
	
	/**
	 * 异步请求树型子节点，返回json数据
	 * 
	 * xml配置了mvc:message-converters，全局编码统一转换，则不需要produces
	 * 
	 * @return 无返回，直接向response里写数据
	 * @throws IOException 
	 */
	@RequestMapping(value="/getSubTree",method=RequestMethod.GET)
	@ResponseBody
	public String getSubTree(String id){
		return service.getSubTree(id).toString();
	}
	
	/**
	 *  getImage 得到图片二进制内容
	 *  
	 *  @ResponseBody注解直接向客户端返回byte数组,框架自动转化为二进制流
	 *  
	 *  @param	String id:pk
	 *  
	 *  @return byte[]
	 */
	@RequestMapping(value="/getImage/{id}",method=RequestMethod.GET)
	@ResponseBody
	public byte[] getImage(@PathVariable("id") String id){
		Organ entity = service.findOne(id);
		return entity.getPhoto();
	}
	
	/**
	 *  getParent 得到父级实体json
	 *  
	 *  @ResponseBody注解直接向客户端实体对象,框架自动转化为json
	 *  
	 *  @param	String id:pk
	 *  
	 *  @return Organ
	 */
	@RequestMapping(value="/getParent",method=RequestMethod.GET)
	@ResponseBody
	public Organ getParent(String id) {
		Organ entity = service.findOne(id);
		return entity;
	}
	
	/**
	 *  初始化新增编辑页
	 */
	@Override
	protected void initAdd(Map<String, Object> map) {
		String order = request.getParameter("order");
		String parentId = request.getParameter("parentId");
		Organ parent = new Organ();
		parent.setId(parentId);
		Organ entity = new Organ();
		entity.setParent(parent);		
		parent = service.findOne(parentId);
		List<Organ> list = new ArrayList<Organ>();
		list.add(parent);
		if(parent.getRegion()!=null) {
			entity.setRegion(parent.getRegion());
		}
		if(StringUtils.isNotBlank(parent.getAddr())) {
			entity.setLongitude(parent.getLongitude());
			entity.setLatitude(parent.getLatitude());
			entity.setAddr(parent.getAddr());
		}
		map.put("entity", entity);
		map.put("list", list);
		map.put("unitNature", ds.getPropertyByEnName("unitNature"));
		map.put("subOrdination", ds.getPropertyByEnName("subOrdination"));
		map.put("classifyToManage", ds.getPropertyByEnName("classifyToManage"));	
		//新增后返回所选树型节点列表
		getSession().setAttribute("organ_type", "redirect");
		getSession().setAttribute("organ_query", entity);
		getSession().setAttribute("organ_order", order);
	}
	
	/**
	 *  初始化更新编辑页
	 */
	@Override
	protected void initEdit(Map<String, Object> map) {
		Organ entity = (Organ)map.get("entity");
		List<Organ> list = new ArrayList<Organ>();
		list.add(entity.getParent());
		map.put("list", list);
		map.put("entity", entity);
		map.put("unitNature", ds.getPropertyByEnName("unitNature"));
		map.put("subOrdination", ds.getPropertyByEnName("subOrdination"));
		map.put("classifyToManage", ds.getPropertyByEnName("classifyToManage"));
	}
	
	/**
	 *  保存前执行,用于保存之前再次设置要保存的实体属性或执行其它保存逻辑
	 * @throws IOException 
	 */
	@Override
	protected Organ beforeSave(Organ entity,MultipartFile file) throws IOException {
		if(file.getSize()>0) {
			entity.setPhoto(ImageUtil.getPhotoBytes(file, 512, 1027, 768));
		}
		return entity;
	}

	/**
	 *  更新前执行,用于更新之前再次设置要更新的实体属性或执行其它更新逻辑
	 */
	@Override
	protected Organ beforeUpdate(Organ entity,MultipartFile file) throws IOException {
		if(file.getSize()>0) {
			entity.setPhoto(ImageUtil.getPhotoBytes(file, 512, 1027, 768));
		}
		return entity;
	}
}
